import "./index.css";
import { useState } from 'react';
import { useHistory } from "react-router-dom";
import axios from 'axios';
function Login() {
    const [username, setUsername] = useState("")
    const [password, setPassword] = useState("")
    const onChangeUsername = (event)=>{
        setUsername(event.target.value)
    }
    const onChangePassword = (event)=>{
        setPassword(event.target.value)
    }
    let history = useHistory();
    const onClickLogin = ()=>{
        axios.post('/V1/user/login',{
            username,
            password
        })
        .then((res)=>{
            console.log(res)
            if(res.data.code===200){
                console.log("登录成功")
                history.push(`/lottery/${username}`);
            }else{
                alert(res.data.msg)
            }
        })
        .catch((err)=>{
            console.log(err)
        })
        // 测试用
        // history.push(`/lottery/${username}`);
    }
    return (
        <div className="container">
            <div className="login-box">
                <h1 className="login-box--header">
                    登录
                </h1>
                <div className="login-box--body">
                    <div className="login-box--body__username">
                        <label>用户名</label>
                        <input type="text" name="username" value={username} onChange={onChangeUsername}/>
                    </div>
                    <div className="login-box--body__password">
                        <label>密码</label>
                        <input type="password" name="password" value={password} onChange={onChangePassword} />
                    </div>
                </div>
                <button onClick={onClickLogin}>登录</button>
            </div>
        </div>
    )
}
export default Login;